<?php
/**
 * news controller
 */
namespace App\Http\ControllersAdmin;

use App\Service\NewsService;
use App\Validation\NewsValidation;
use App\Validation\NewsSearchValidation;
use App\Validation\DeleteValidation;
use App\Validation\BatchValidation;

class NewsController extends BaseController
{
	/**
	 * [indexAction description]
	 * 2017-11-13 调整为连接池 提高查询速度
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new NewsSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$d = [];

		$data = NewsService::getSearch($raw);

		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->news . $value->id;
			$d[$key]->pics = explode(',', $value->pics);
			$d[$key]->pics = array_filter($d[$key]->pics);
		}

		$count = NewsService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count[0]->cou,
			'list'  => $d
		];
	
		return $this->echoJson($assign);
	}

	/**
	 * [recoAction 最新推荐列表]
	 * @return [type] [description]
	 */
	public function recoAction()
	{
		$this->getUser();
		$raw = $this->validation(new NewsSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->reco_by = 2;
		$d = [];
		$data = NewsService::getSearch($raw);
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->news . $value->id;
			$d[$key]->pics = explode(',', $value->pics);
			$d[$key]->pics = array_filter($d[$key]->pics);
		}		
		$count = NewsService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [firstAction 首页列表]
	 * @return [type] [description]
	 */
	public function firstAction()
	{
		$this->getUser();
		$raw = $this->validation(new NewsSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->index_by = 2;
		$d = [];
		$data = NewsService::getSearch($raw);
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->news . $value->id;
			$d[$key]->pics = explode(',', $value->pics);
			$d[$key]->pics = array_filter($d[$key]->pics);
		}		
		$count = NewsService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new NewsValidation, $this->request->getJsonRawBody());
		$raw->id = NULL;
		if (isset($raw->pics) && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}
		if (isset($raw->reco_ids) && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}

		$data = NewsService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [stickAction 置顶]
	 * @return [type] [description]
	 */
	public function stickAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		NewsService::getStick($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsService::getModel($raw->id);

		$arr = new \stdClass();
		$go_url = $this->config->gourl . $this->config->urls->news . $data->id;

		if ($data){
			$arr = new \stdClass();
			foreach ($data as $key => $value) {
				$arr->$key = $value;
			}
			if ($data->pics){
				$arr->pics = explode(",", $data->pics);	
			}else{
				$arr->pics = [];
			}
			$arr->three_url = $data->three_url ? $data->three_url : $go_url;
			$arr->album = (array)json_decode($data->album);
			$arr->reco_ids = NewsService::getRecoInfo($raw->id);
		}
		
		return $this->echoJson($arr);
	}

	/**
	 * [setrecoAction 执行推荐  将video数据列表推荐到首页-banner]
	 * @return [type] [description]
	 */
	public function setrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		NewsService::batchReco($raw->id, 2);
		return $this->echoJson("ok");
	}

	/**
	 * [delrecoAction 执行取消推荐]
	 * @return [type] [description]
	 */
	public function delrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		NewsService::batchReco($raw->id);
		return $this->echoJson("ok");
	}

	/**
	 * [setrecoAction 执行首页 将video数据列表推荐到首页-video]
	 * @return [type] [description]
	 */
	public function setfirstAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		NewsService::batchReco($raw->id, 1);
		return $this->echoJson("ok");
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$raw = $this->validation(new NewsValidation, $this->request->getJsonRawBody());

		if (isset($raw->pics) && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}
		if (isset($raw->reco_ids) && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}

		$data = NewsService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [upAction 上架]
	 * @return [type] [description]
	 */
	public function displayAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsService::display($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsService::delete($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [foreverAction 彻底删除]
	 * @return [type] [description]
	 */
	public function foreverAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NewsService::forever($raw->id);
		return $this->echoJson('ok');
	}
}